package com.wtwd.campus.h5_manager.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.h5_manager.common.Constant;
import com.wtwd.campus.h5_manager.dao.H5CustomerMapper;
import com.wtwd.campus.h5_manager.entity.H5Customer;
import com.wtwd.campus.h5_manager.entity.vo.H5CustomerVo;
import com.wtwd.campus.h5_manager.service.H5CustomerService;
import com.wtwd.campus.h5_manager.service.H5ManageRoleService;
import com.wtwd.campus.h5_manager.service.H5ManagerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2022-03-07
 */
@Service
public class H5CustomerServiceImpl implements H5CustomerService {

    @Autowired
    private H5CustomerMapper customerMapper;

    @Autowired
    private H5ManageRoleService manageRoleService;

    @Autowired
    private H5ManagerUserService userService;

    @Override
    public H5Customer getH5CustomerById(int id) {
        return new LambdaQueryChainWrapper<>(customerMapper)
                .eq(H5Customer::getId, id)
                .one();
    }

    @Override
    public H5Customer getH5CustomerByName(String name) {
        return new LambdaQueryChainWrapper<>(customerMapper)
                .eq(H5Customer::getCustomerName, name)
                .one();
    }

    @Override
    public Integer addCustomer(H5Customer h5Customer) {
        return customerMapper.addCustomer(h5Customer);
    }

    @Override
    public IPage<H5CustomerVo> queryCustomerPage(Map<String, Object> searchMap, Page<H5CustomerVo> page) {
        return customerMapper.queryCustomerPage(searchMap, page);
    }

    @Override
    public Boolean authCustomerName(H5CustomerVo h5CustomerVo) {
        H5Customer customer = this.getH5CustomerByName(h5CustomerVo.getCustomerName());
        if (h5CustomerVo.getId() == null) {//添加鉴别
            if (customer != null) {
                return false;
            }
        } else {//修改鉴别
            if (customer != null) {
                if (!h5CustomerVo.getId().equals(customer.getId())) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public Integer updateCustomerById(H5Customer customer) {
        return customerMapper.updateById(customer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteById(Integer customerId) {
        //删除该客户下所有角色
        manageRoleService.deleteByCustomerId(customerId);
        //删除该客户下所有账号
        userService.deleteByCustomerId(customerId);
        //删除客户
        return customerMapper.deleteById(customerId);
    }

    @Override
    public List<H5Customer> queryCustomer(Integer customerId) {
        List<H5Customer> h5Customers = new ArrayList<>();
        if (customerId.equals(Constant.H5_MANAGE_CUSTOMER_ADMIN)) {
            h5Customers = new LambdaQueryChainWrapper<>(customerMapper)
                    .list();
        } else {
            H5Customer h5Customer = this.getH5CustomerById(customerId);
            if (h5Customer != null) {
                h5Customers.add(h5Customer);
            }
        }
        return h5Customers;
    }
}
